
layui.use(['form', 'layedit', 'laydate',"table",'upload'], function() {
    var $ = layui.jquery
        , upload = layui.upload;
    var table = layui.table;
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;
    //新增按钮
    $('#add').click(function () {
        var  width=($(window).width()*0.5);
        var height=($(window).height()*0.4);
        layer.open({
            type: 1,
            closeBtn: 1,
            area: [width + 'px', height + 'px'],//大小
            content: $("#addUpdate"),
            skin: "layui-layer-molv",
            title:  "新增热搜词"
        })
    })


    //监听工具条
    table.on('tool(demo)', function(obj){
        var data = obj.data;
        if(obj.event === 'detail'){

        } else if(obj.event === 'del'){
            layer.confirm('确定删除吗?', function(index){
                $.ajax({
                    url:"/searchController/delById",
                    type:"POST",
                    data:{id:data.id},
                    success:function(data,status,xhr){
                        if (xhr.code == -1){
                            top.location.href="/pageController/login"

                        }
                        if(xhr.status == 204){
                            layer.alert('删除成功', {icon: 1}, function(index){
                                layer.closeAll("page");
                                layer.close(index);
                                var curr = sessionStorage.getItem("curr");
                                table.reload('testReload',{page:{curr:curr}});
                            });
                        }else if (xhr.status == 400){
                            layer.alert('参数异常', {icon: 5});
                        }else if (xhr.status == 500){
                            layer.alert('操作失败', {icon: 5});
                        }

                    }
                })
                layer.close(index);

            });
        }
    });

    //方法级渲染
    table.render({
        elem: '#table'
        ,url: '/searchController/paramlist'
        ,cols: [
            [
                {type:'numbers', title: '序',align:"center"},

                { field: 'name', title: '热搜词',align:"center"},

                { field: 'searchTimeIn3Day', title: '最近3天搜索次数',align:"center"},

                { field: 'searchTimeIn7Day', title: '最近7天搜索次数',align:"center"},

                { field: 'searchCount', title: '搜索总量',align:"center"},

                {field: null ,width:"25%", title: '操作',templet:"#barDemo",align:"center" }
            ]
        ]
        ,id: 'testReload'
        ,page: true
        ,done: function(res, curr, count){
            sessionStorage.setItem("count",count)
            sessionStorage.setItem("curr",curr)
        }

    });



    $('.demoTable .layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //自定义验证规则
    form.verify({
        title: function(value){
            if(value.length < 1 ||value.length >10 ){
                return '热搜词在10个字符以内';
            }
            if(!new RegExp("^[A-Za-z0-9\u4e00-\u9fa5]+$").test(value)&&value != null&&value != ""){
                return  "热搜词不能包含特殊字符和表情"
            }
        },
        search: function (value) {
            if(!new RegExp("^[+]{0,1}(\\d+)$").test(value)){
                return '热搜词排序为正整数';
            }
            if (value >999999999){
                return '热搜词排序数过大';

            }
        }



    });

    //提交新增与更新
    form.on('submit(addUpdate)', function(data){
       var count = sessionStorage.getItem("count");
       if (count == 10){
           layer.alert('热搜词最多为10个', {icon: 5});
           return false
       }


        $.ajax({
            url:"/searchController/insert",
            type:"POST",
            data:data.field,
            // dataType:"json",
            success:function(data,status,xhr){

                if(xhr.status == 201) {
                    layer.alert('添加成功', {icon: 1}, function (index) {
                        layer.closeAll("page");
                        layer.close(index);
                        var curr = sessionStorage.getItem("curr");
                        table.reload('testReload',{page:{curr:curr}});
                    });
                }else if (xhr.status == 400){
                    layer.alert('热搜词必填', {icon: 5});

                }else{
                    layer.alert('操作失败', {icon: 5});
                }


            },
            error:function (data,status,xhr) {

                layer.alert('系统出错!', {icon: 5});
            }
        })
        return false;
    });




});





